/*
 * Copyright (C) 2010 Denis Mingulov.
 *
 * This file is part of QReader QtWebView test.
 *
 * Author: Denis Mingulov <denis@mingulov.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */


#include "filelist.h"
#include "ui_filelist.h"
#include "filelistmodel.h"
#include <QResizeEvent>
#include <QLibraryInfo>

FileList::FileList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FileList)
{
    ui->setupUi(this);

    FileListModel *model = new FileListModel(this);

    // connect Path label to auto update
    connect(model, SIGNAL(directoryIsChanged(QString)), ui->lineEdit, SLOT(setText(QString)));
//    connect(ui->lineEdit, SIGNAL(textChanged(QString)), model, SLOT(setDirPath(QString)));
    connect(ui->listView, SIGNAL(clicked(QModelIndex)), model, SLOT(itemClicked(QModelIndex)) );

    // set up a model
    ui->listView->setModel(model);

    // set focus (for non touch devices)
    ui->listView->setFocus();

    model->setDirPath(QLibraryInfo::location(QLibraryInfo::PrefixPath));
}

FileList::~FileList()
{
    if(ui && ui->listView)
        delete ui->listView->selectionModel();
    delete ui;
}

void FileList::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void FileList::resizeEvent(QResizeEvent *event)
{
    const QSize newSize = event->size();
//    ui->verticalLayout->setGeometry(QRect(0,0, newSize.width(), newSize.height()));
    ui->verticalLayoutWidget->resize(newSize);
    ui->verticalLayoutWidget->move(0,0);
}
